package com.example.doctor.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;

public interface DoctorCenterMapper {
    @SelectProvider(type = SqlBuilder.class, method = "IMOrderListSQL")
	@Results(id = "IMOrderListResult", value = { 
            @Result(property = "OrderNO", column = "order_no_nvarchar", id = true),
			@Result(property = "OCStatus", column = "oc_status_tinyint"),
			@Result(property = "Name", column = "Name_nvarchar"),
			@Result(property = "CreateTime", column = "order_pay_time_datetime") })
	List<Map<String, Object>> IMOrderList(
        @Param("UID") long UID, @Param("AType") int AType, 
        @Param("skip") int skip, @Param("limit") int limit);

        @SelectProvider(type = SqlBuilder.class, method = "DocATypeCountSQL")
        Map<String, Object> DocATypeCount(@Param("UID") long UID, @Param("AType") int AType, @Param("NowDate") String NowDate);

    class SqlBuilder{
        public String IMOrderListSQL(
            @Param("UID") long UID, @Param("AType") int AType,
            @Param("skip") int skip, @Param("limit") int limit) {
			String sql = "select a.order_no_nvarchar,a.oc_status_tinyint,b.Name_nvarchar,a.order_pay_time_datetime"
            + " from order_main a"
            + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " where a.dc_id_bigint = #{UID} and a.system_status_tinyint = 2 and a.a_type_int = #{AType}"
            + " order by a.order_pay_time_datetime desc"
            + " limit #{skip},#{limit}";
			return sql;
        }
        
        public String DocATypeCountSQL(@Param("UID") long UID, @Param("AType") int AType, @Param("NowDate") String NowDate){
            String sql = "select #{AType} as AType,a.ToBeCount,b.FinCount,c.TodayAddCount,d.ServiceAllCount from "
            + " (select #{UID} as DCID_bigint,count(1) as ToBeCount from im_online_consultation where dc_id_bigint = #{UID} and oc_status_tinyint = 0 and a_type_int = #{AType}) a"
            + " inner join (select #{UID} as DCID_bigint,count(1) as FinCount from im_online_consultation where dc_id_bigint = #{UID} and oc_status_tinyint = 2 and a_type_int = #{AType}) b on a.DCID_bigint = b.DCID_bigint"
            + " inner join (select #{UID} as DCID_bigint,count(1) as TodayAddCount from im_online_consultation where dc_id_bigint = #{UID} and create_time_datetime > #{NowDate} and a_type_int = #{AType}) c on a.DCID_bigint = c.DCID_bigint"
            + " inner join (select #{UID} as DCID_bigint,count(1) as ServiceAllCount from im_online_consultation where dc_id_bigint = #{UID} and oc_status_tinyint in (1,2) and a_type_int = #{AType}) d on a.DCID_bigint = d.DCID_bigint";
            return sql;
        }
    }
}


